package com.wuge.gulimall.ware.service.impl;

import com.wuge.gulimall.ware.constant.PurchaseStatus;
import com.wuge.gulimall.ware.constant.WareConstant;
import com.wuge.gulimall.ware.entity.PurchaseDetailEntity;
import com.wuge.gulimall.ware.service.PurchaseDetailService;
import com.wuge.gulimall.ware.vo.PurchaseDoneVo;
import com.wuge.gulimall.ware.vo.PurchaseItemDoneVo;
import com.wuge.gulimall.ware.vo.PurchaseMergeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuge.common.utils.PageUtils;
import com.wuge.common.utils.Query;

import com.wuge.gulimall.ware.dao.PurchaseDao;
import com.wuge.gulimall.ware.entity.PurchaseEntity;
import com.wuge.gulimall.ware.service.PurchaseService;
import org.springframework.util.StringUtils;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {
    @Autowired
    private WareSkuServiceImpl wareSkuService;
    @Autowired
    private PurchaseDetailService purchaseDetailService;
    @Override
    public void received(List<Long> ids) {
        List<PurchaseEntity> list = ids.stream().map((id) -> {
            return getById(id);
        }).collect(Collectors.toList());
        list.stream().forEach((entity -> {
                    entity.setUpdateTime(new Date());
                    entity.setStatus(2);
                    purchaseDetailService.updateStatusById(entity.getId());
                })
        );
        updateBatchById(list);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnrceive(Map<String, Object> params) {
        QueryWrapper<PurchaseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("status", PurchaseStatus.CREATE.getCode()).or().eq("status", PurchaseStatus.ASSIGNED.getCode());
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
         wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void merge(PurchaseMergeVo purchase) {
        Long purchaseId = purchase.getPurchaseId();
        if (purchaseId==null){
            PurchaseEntity purchaseEntity=new PurchaseEntity();
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setStatus(0);
            save(purchaseEntity);
            purchaseId=purchaseEntity.getId();
        }
        List<Long> items = purchase.getItems();
        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> collect = items.stream().map((entity) -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            purchaseDetailEntity.setId(entity);
            purchaseDetailEntity.setStatus(1);
            purchaseDetailEntity.setPurchaseId(finalPurchaseId);
            return purchaseDetailEntity;
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(collect);
        PurchaseEntity purchaseEntity=new PurchaseEntity();
        purchaseEntity.setUpdateTime(new Date());
        purchaseEntity.setId(purchaseId);
        updateById(purchaseEntity);
    }
    @Override
    public void done(PurchaseDoneVo doneVo) {

        Long id = doneVo.getId();

        //1、改变采购项的状态
        Boolean flag = true;
        List<PurchaseItemDoneVo> items = doneVo.getItems();
        List<PurchaseDetailEntity> updates = new ArrayList<>();

        for (PurchaseItemDoneVo item : items) {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            if (item.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()) {
                flag = false;
                purchaseDetailEntity.setStatus(item.getStatus());
            } else {
                purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.FINISH.getCode());
                //3、将成功采购的进行入库
                //查出当前采购项的详细信息
                //PurchaseDetailEntity entity = purchaseDetailService.getById(item.getItemId());
                PurchaseDetailEntity entity = purchaseDetailService.getById(item.getItemId());
                wareSkuService.addStock(entity.getSkuId(),entity.getWareId(),entity.getSkuNum());

            }
            purchaseDetailEntity.setId(item.getItemId());

            updates.add(purchaseDetailEntity);
        }

        //批量更新
        purchaseDetailService.updateBatchById(updates);

        //2、改变采购单状态
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(id);
        purchaseEntity.setStatus(flag?WareConstant.PurchaseStatusEnum.FINISH.getCode():WareConstant.PurchaseStatusEnum.HASERROR.getCode());
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);

    }
}